<?php

namespace App\Service\Cruise\Generic;

use App\Models\Cruise\CruiseOrderModel;
use Intervention\Image\Facades\Image;

class CreateWatermarkPng
{

    /**
     * 生成水印图片（未上传OSS）
     * @param string $time
     * @param  $info
     * @param string $address
     * @param string $name
     * @param $module
     * @return string
     */
    public static function getWatermarkPng(string $time, $info, string $address, string $name, $module): string
    {
        switch ($module) {
            case CruiseOrderModel::TYPE_COMPANY_CHECK:
                $module = '工厂名称';
                break;
            case CruiseOrderModel::TYPE_CHECK:
                $module = '门店编号';
                break;
            default:
                $info   = '';
                $module = '';
        }
        $affressLength   = ((mb_strlen($address) - 9) > 0) ? (mb_strlen($address) - 9) : 0;
        $nameLength      = ((mb_strlen($name) - 9) > 0) ? (mb_strlen($name) - 9) : 0;
        $watermarkWidth  = 2500 + (170 * max($affressLength, $nameLength));
        $watermarkHeight = 1000;
        $img             = Image::make(public_path('images/watermarkBackground.png'));
        $img->resize($watermarkWidth, $watermarkHeight);
        $img->text('拍摄时间：' . $time, 100, 230, function ($font) {
            $font->file(public_path('watermark.ttf'));
            $font->size(166);
            $font->color('#FFFFFF');
        })->text($module . '：' . $info, 100, 450, function ($font) {
            $font->file(public_path('watermark.ttf'));
            $font->size(166);
            $font->color('#FFFFFF');
        })->text('拍摄地点：' . $address, 100, 660, function ($font) {
            $font->file(public_path('watermark.ttf'));
            $font->size(166);
            $font->color('#FFFFFF');
        })->text('拍摄人员：' . $name, 100, 880, function ($font) {
            $font->file(public_path('watermark.ttf'));
            $font->size(166);
            $font->color('#FFFFFF');
        });
        $file = md5(time());
        $img->save(sys_get_temp_dir() . '/' . $file . '.png');
        return sys_get_temp_dir() . '/' . $file . '.png';
    }


}
